package com.pyg.goods.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.pyg.goods.service.TbBrandService;
import com.pyg.mapper.TbBrandMapper;
import com.pyg.pojo.TbBrand;
import com.pyg.pojo.TbBrandExample;
import com.pyg.pojo.TbBrandExample.Criteria;
import com.pyg.utils.PygStringUtils;

import entity.PageResult;

/**
 * Created by zhuge on 2017/10/12.
 */
@Service
public class BrandServiceImpl implements TbBrandService {
	@Autowired
	private TbBrandMapper tbBrandMapper;

	@Override
	public List<TbBrand> findAll() {
		return tbBrandMapper.selectByExample(null);
	}

	@Override
	public PageResult findPage(int pageNum, int pageSize) {
		// 开启分页插件功能
		PageHelper.startPage(pageNum, pageSize);

		Page<TbBrand> page = (Page<TbBrand>) tbBrandMapper.selectByExample(null);

		return new PageResult(page.getTotal(), page.getResult());
	}

	/**
	 * 品牌条件分页
	 *
	 * @param tbBrand
	 *            查询条件
	 * @param pageNum
	 *            当前页面
	 * @param pageSize
	 *            每页记录数
	 * @return
	 */
	@Override
	public PageResult findPage(TbBrand tbBrand, int pageNum, int pageSize) {
		// 开启分页插件功能
		PageHelper.startPage(pageNum, pageSize);

		TbBrandExample example = null;
		if (tbBrand != null) {
			example = new TbBrandExample();
			Criteria criteria = example.createCriteria();
			if (PygStringUtils.isNotEmpty(tbBrand.getName())) {
				criteria.andNameLike("%" + tbBrand.getName() + "%");
				System.out.println("tbBrand.getName():"+tbBrand.getName());
			}
			if (PygStringUtils.isNotEmpty(tbBrand.getFirstChar())) {
				criteria.andFirstCharEqualTo(tbBrand.getFirstChar());
				System.out.println("tbBrand.getFirstChar():"+tbBrand.getFirstChar());
			}
			System.out.println(tbBrand);

		}

		Page<TbBrand> page = (Page<TbBrand>) tbBrandMapper.selectByExample(example);

		return new PageResult(page.getTotal(), page.getResult());
	}

	@Override
	public void add(TbBrand tbBrand) {
		tbBrandMapper.insert(tbBrand);
	}

	@Override
	public void del(Long[] ids) {
		for (Long id : ids) {
			tbBrandMapper.deleteByPrimaryKey(id);
		}
	}

	@Override
	public TbBrand findOne(Long id) {
		return tbBrandMapper.selectByPrimaryKey(id);
	}

	@Override
	public void update(TbBrand tbBrand) {
		tbBrandMapper.updateByPrimaryKey(tbBrand);
	}

	/**
	 * 品牌下拉框数据
	 *
	 * @return
	 */
//	@Override
//	public List<Map> selectOptionList() {
//		// return tbBrandMapper.selectOptionList();
//		return new ArrayList<Map>();
//	}

}
